﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pandemia.Entidades;
using System.ServiceModel;
using System.ServiceModel.Description;
using Pandemia.Servicios;
using System.Configuration;
using System.IO;

namespace Pandemia.Funcionalidad
{
    public class Pandemonium
    {
        #region Config

        private Configuracion _configuracion;
        public Configuracion Configuracion 
        {
            get
            {
                if (_configuracion == null)
                    _configuracion = new Configuracion();

                return _configuracion;
            }

            set { _configuracion = value; }
        }

        public void RefrescarConfiguracion()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\svrcfg.txt";

            if (File.Exists(path))
                Configuracion = Configuracion.Abrir(path);
            else
            {
                Configuracion.SoporteServiceUrl = ConfigurationManager.AppSettings["SoporteServiceUrl"];
                Configuracion.TransferenciaServiceUrl = ConfigurationManager.AppSettings["TransferenciaServiceUrl"];
                Configuracion.Contraseña = ConfigurationManager.AppSettings["Contraseña"];
                Configuracion.MinutosValidezContraseña = 1;
                Configuracion.CarpetaTransferencias = ConfigurationManager.AppSettings["CarpetaTransferencias"];
            }

            PandemServidor servidor = PandemServidor.ObtenerInstancia();
            servidor.Contraseña = Configuracion.Contraseña;
            servidor.MinutosValidezContraseña = Configuracion.MinutosValidezContraseña;
            servidor.CarpetaTransferencias = Configuracion.CarpetaTransferencias;
        }

        #endregion

        #region Servicios

        private List<ServiceHost> _hosts;

        private void IniciarServicio<Tipo>(string url, bool streamed)
        {
            Uri baseAddress = new Uri(url);
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            ServiceHost host = new ServiceHost(typeof(Tipo), baseAddress);

            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            if (streamed)
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                binding.Name = @"MTOMBinding";
                binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = 999999999;
                binding.MessageEncoding = WSMessageEncoding.Mtom;
                binding.TransferMode = TransferMode.Streamed;
                host.AddServiceEndpoint(typeof(Tipo), binding, @"");
                host.AddServiceEndpoint(typeof(IMetadataExchange), binding, @"MEX");
            }

            host.Open();
            _hosts.Add(host);
        }

        public void IniciarServicios()
        {
            if (_hosts == null)
                _hosts = new List<ServiceHost>();

            IniciarServicio<SoporteService>(Configuracion.SoporteServiceUrl, false);
            IniciarServicio<TransferenciaService>(Configuracion.TransferenciaServiceUrl, true);
        }

        #endregion
    }
}
